package com.example.recyclerviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private List<Fruit> fruitList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        RecyclerView recycleView=(RecyclerView)findViewById(R.id.recycler_view);
        //LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recycleView.setLayoutManager(layoutManager);
        FruitAdapter adapter=new FruitAdapter(fruitList);
        recycleView.setAdapter(adapter);
    }
    public void initFruits(){
        for(int i=0;i<3;i++){
            Fruit apple=new Fruit(getRandomLengthName("Apple"),R.mipmap.apple_pic);
            fruitList.add(apple);
            Fruit banana=new Fruit(getRandomLengthName("banana"),R.mipmap.banana_pic);
            fruitList.add(banana);
            Fruit orange=new Fruit(getRandomLengthName("orange"),R.mipmap.orange_pic);
            fruitList.add(orange);
            Fruit watermelon=new Fruit(getRandomLengthName("watermelon"),R.mipmap.watermelon_pic);
            fruitList.add(watermelon);
            Fruit pear=new Fruit(getRandomLengthName("pear"),R.mipmap.pear_pic);
            fruitList.add(pear);
            Fruit grape=new Fruit(getRandomLengthName("grape"),R.mipmap.grape_pic);
            fruitList.add(grape);
            Fruit strawberry=new Fruit(getRandomLengthName("strawberry"),R.mipmap.strawberry_pic);
            fruitList.add(strawberry);
            Fruit cherry=new Fruit(getRandomLengthName("cherry"),R.mipmap.cherry_pic);
            fruitList.add(cherry);
            Fruit mango=new Fruit(getRandomLengthName("mango"),R.mipmap.mango_pic);
            fruitList.add(mango);
        }
    }
    private String getRandomLengthName(String name){
        Random random =new Random();
        int length=random.nextInt(20)+1;
        StringBuilder builder=new StringBuilder();
        for(int i=0;i<length;i++){
            builder.append(name);
        }
        return builder.toString();
    }
}
